package com.jtjrenren.android.taxi.passenger.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.db.DbBaseDao;
import com.jtjrenren.android.taxi.passenger.bean.AddressInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wdl on 2016/1/25.
 */
public class AddressDao extends DbBaseDao {
    private static final String TABLE_NAME = "address";

    private String[] columns = {"id", "name", "addr", "lati", "loni", "type", "count"};

    //create table address(id integer primary key autoincrement,
    // name varchar(50),addr varchar(60),lati varchar(20),loni varchar(20),type integer,count integer)
    public AddressDao(Context context) {
        super(context, TABLE_NAME);
    }

    public long addNewAddress(AddressInfo info) {

        if (info.getType() == Constants.CONFIG_ADDR_TYPE_COMPANY
                || info.getType() == Constants.CONFIG_ADDR_TYPE_HOME
                || info.getType() == Constants.CONFIG_ADDR_TYPE_OTHER) {
            AddressInfo addrInfoByType = getAddrInfoByType(info.getType());
            if (addrInfoByType != null) {
                info.setId(addrInfoByType.getId());
                return updateAddressInfoById(info);
            }
        } else {
            AddressInfo addrInfoByLatLng = getAddrInfoByLatLng(info.getLati(), info.getLongi());
            if (addrInfoByLatLng != null && info.getType() == addrInfoByLatLng.getType()) {
                addrInfoByLatLng.setCount(info.getCount() + 1);
                return updateAddressInfoById(addrInfoByLatLng);
            }
        }

        ContentValues values = getContentValuesByAddrInfo(info);

        return insert(null, values);
    }

    public int updateAddressInfoById(AddressInfo info) {
        ContentValues contentValuesByAddrInfo = getContentValuesByAddrInfo(info);
        return update(contentValuesByAddrInfo, "id=?", new String[]{info.getId()});
    }


    public List<AddressInfo> getAddrListByType(int type) {
        List<AddressInfo> list = new ArrayList<>();
        Cursor query = query(columns, "type=?", new String[]{type + ""}, "count desc");
        AddressInfo info;
        while (query.moveToNext()) {
            info = getAddrInfoByCursor(query);

            list.add(info);
        }
        query.close();
        closeDb();
        return list;
    }

    public AddressInfo getAddrInfoByLatLng(String lati, String loni) {
        Cursor cursor = query(columns, "lati=? and loni=?", new String[]{lati, loni}, null);
        AddressInfo info = null;
        if (cursor.moveToNext()) {
            info = getAddrInfoByCursor(cursor);
            info.setCount(6);
        }
        cursor.close();
        closeDb();
        return info;

    }

    public AddressInfo getAddrInfoByType(int type) {
        Cursor cursor = query(columns, "type=?", new String[]{type + ""}, null);
        AddressInfo info = null;
        if (cursor.moveToNext()) {
            info = getAddrInfoByCursor(cursor);
            info.setCount(6);
        }
        cursor.close();
        closeDb();
        return info;

    }

    private AddressInfo getAddrInfoByCursor(Cursor cursor) {
        AddressInfo info;

        info = new AddressInfo();
        info.setId(cursor.getString(0));
        info.setName(cursor.getString(1));
        info.setAddr(cursor.getString(2));
        info.setLati(cursor.getString(3));
        info.setLongi(cursor.getString(4));
        info.setType(cursor.getInt(5));

        return info;
    }

    private ContentValues getContentValuesByAddrInfo(AddressInfo info) {
        ContentValues values = new ContentValues();
        values.put("name", info.getName());
        values.put("addr", info.getAddr());
        values.put("lati", info.getLati());
        values.put("loni", info.getLongi());
        values.put("type", info.getType());

        return values;
    }
}
